#!/bin/bash
#
#********************************************************************
#Description：        回滚 dim 层的拉链表 dim_trade_shops
#
source /etc/profile
if [ -n "$1" ]
then
  rollback_date=$1
else
  rollback_date=`date -d "-1 day" +%F`
fi

sql="
DROP TABLE IF EXISTS tmp.shops_tmp_$rollback_date;
CREATE TABLE IF NOT EXISTS tmp.tmp_shops_$rollback_date AS
SELECT shopid,
       userid,
       areaid,
       shopname,
       shoplevel,
       status,
       createtime,
       modifytime,
       startdate,
       enddate
FROM dim.dim_trade_shops
WHERE enddate < '$rollback_date'
UNION ALL
SELECT shopid,
       userid,
       areaid,
       shopname,
       shoplevel,
       status,
       createtime,
       modifytime,
       startdate,
       '9999-12-31' AS enddate
FROM dim.dim_trade_shops
WHERE startdate <= '$rollback_date}'
  AND enddate >= '$rollback_date';

INSERT OVERWRITE TABLE dim.dim_trade_shops
SELECT *
FROM tmp.tmp_shops_$rollback_date;
"

hive -e "$sql"